import request from './request.js'

// 用户相关API
export const userApi = {
  // 发送验证码
  sendCode: (email) => {
    return request({
      url: '/user/sendCode',
      method: 'POST',
      data: email,
      headers: {
        'Content-Type': 'text/plain'
      }
    })
  },

  // 用户注册
  register: (user) => {
    return request({
      url: '/user/register',
      method: 'POST',
      data: user
    })
  },

  // 用户登录
  login: (user) => {
    return request({
      url: '/user/login',
      method: 'POST',
      data: user
    })
  },

  // 用户退出登录
  logout: () => {
    return request({
      url: '/user/logout',
      method: 'GET'
    })
  },

  // 获取当前登录用户信息
  getCurrentUser: () => {
    return request({
      url: '/user',
      method: 'GET'
    })
  },

  // 获取用户列表
  getUserList: (params) => {
    return request({
      url: '/users',
      method: 'GET',
      params
    })
  },
  
  // 获取用户详情
  getUserById: (id) => {
    return request({
      url: `/users/${id}`,
      method: 'GET'
    })
  },
  
  // 创建用户
  createUser: (data) => {
    return request({
      url: '/users',
      method: 'POST',
      data
    })
  },
  
  // 更新用户
  updateUser: (id, data) => {
    return request({
      url: `/users/${id}`,
      method: 'PUT',
      data
    })
  },
  
  // 删除用户
  deleteUser: (id) => {
    return request({
      url: `/users/${id}`,
      method: 'DELETE'
    })
  }
}

// 认证相关API
export const authApi = {
  // 用户登录
  login: (data) => {
    return request({
      url: '/auth/login',
      method: 'POST',
      data
    })
  },
  
  // 用户注册
  register: (data) => {
    return request({
      url: '/auth/register',
      method: 'POST',
      data
    })
  },
  
  // 获取当前用户信息
  getCurrentUser: () => {
    return request({
      url: '/auth/me',
      method: 'GET'
    })
  },
  
  // 用户登出
  logout: () => {
    return request({
      url: '/auth/logout',
      method: 'POST'
    })
  }
}

// 会话管理API
export const sessionApi = {
  // 获取所有会话
  getAllSessions: () => {
    return request({
      url: '/session',
      method: 'GET'
    })
  },

  // 创建新会话
  createSession: (topic) => {
    return request({
      url: '/session',
      method: 'POST',
      params: { topic }
    })
  },

  // 更新会话
  updateSession: (sessionData) => {
    return request({
      url: '/session',
      method: 'PUT',
      data: sessionData
    })
  },

  // 删除会话
  deleteSession: (sessionId) => {
    return request({
      url: `/session/${sessionId}`,
      method: 'DELETE'
    })
  },

  // 获取会话聊天记录
  getChatHistory: (chatSessionID) => {
    return request({
      url: `/chat/${chatSessionID}`,
      method: 'GET'
    })
  }
}

// 语音对话API
export const talkApi = {
  // 语音对话演示
  talkDemo: (audioFile) => {
    const formData = new FormData()
    formData.append('audio', audioFile)
    
    return request({
      url: '/talk/demo',
      method: 'POST',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 会话语音对话
  talkInSession: (audioFile, sessionId) => {
    const formData = new FormData()
    formData.append('audio', audioFile)
    formData.append('chat_session_id', sessionId)
    
    return request({
      url: '/talk',
      method: 'POST',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 获取消息对应的语音URL
  getAudioUrl: (messageId, content, userId = null) => {
    const params = new URLSearchParams({
      id: messageId,
      content: content
    })
    
    if (userId) {
      params.append('userId', userId)
    }
    
    return request({
      url: `/talk/replay?${params.toString()}`,
      method: 'GET'
    })
  }
}

// 示例API（保持兼容性）
export const exampleApi = {
  // 获取数据列表
  getDataList: (params) => {
    return request({
      url: '/data',
      method: 'GET',
      params
    })
  },
  
  // 上传文件
  uploadFile: (file) => {
    const formData = new FormData()
    formData.append('file', file)
    
    return request({
      url: '/upload',
      method: 'POST',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

// 导出所有API
export default {
  userApi,
  authApi,
  exampleApi,
  sessionApi,
  talkApi
}